package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/***
 *用户接口
 */
public interface UserDao {
    /***
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    User findUserByUsername(String username);




    /**
     *@author TanLin
     * @param queryString
     * @return
     */
    Page<User> findPageUser(String queryString);

    /**
     * 获取所有角色
     * @return
     */
    @Select("select * from t_role")
    List<Role> findRoleAll();

    /**
     * 添加用户
     * @param user
     */
    @SelectKey(statement = "SELECT LAST_INSERT_ID()",keyProperty = "id",keyColumn = "id",before = false,resultType = Integer.class)
    @Insert("INSERT INTO t_user (username,password,gender,remark,station,telephone) " +
            "VALUES (#{username},#{password},#{gender},#{remark},#{station},#{telephone})")
    void addUser(User user);


    /**
     * 添加用户与角色得中间表
     * @param id
     * @param checkitemId
     */
    @Insert("INSERT INTO t_user_role (user_id,role_id) VALUES (#{user_id},#{role_id})")
    void addUserRole(@Param("user_id") Integer id, @Param("role_id") Integer checkitemId);

    /**
     * 编辑回显
     * @param id
     * @return
     */
    @Select("select * from t_user where id = #{id}")
    User findUserById(Integer id);

    /**
     * 查询用户关联得角色集合
     * @param id
     * @return
     */
    @Select("select role_id from t_user_role where user_id = #{user_id}")
    List<Integer> findUserIdsByRoleId(Integer id);

    /**
     * 提交编辑回显数据
     * @param user
     */
    @UpdateProvider(type = update.class,method = "updateGrp")
    void editUser(User user);

    class update {
        public String updateGrp(User user) {
            SQL sql = new SQL().UPDATE("t_user");
            if (user.getGender() != null) {
                sql.SET("gender=#{gender}");
            }
            if (user.getUsername() != null && !"".equals(user.getUsername())) {
                sql.SET("username=#{username}");
            }
            if (user.getRemark() != null) {
                sql.SET("remark=#{remark}");
            }
            if (user.getTelephone() != null) {
                sql.SET("telephone=#{telephone}");
            }
//            if (user.getPassword() != null) {
//                sql.SET("password=#{password}");
//            }
            sql.WHERE("id=#{id}");
            return sql.toString();
        }
    }

    /**
     * 删除id在中间表得所有关联
     * @param id
     */
    @Delete("DELETE FROM t_user_role WHERE user_id = #{user_id}")
    void deleteUserIdRole(Integer id);

    /**
     * @author TanLin
     * 重新添加中间表字段
     * @param id
     * @param checkitemId
     */
    @Insert("INSERT INTO t_user_role (user_id,role_id) VALUES (#{user_id},#{role_id})")
    void editUserIdRole(@Param("user_id") Integer id, @Param("role_id") Integer checkitemId);


    /**
     * 删除主表字段
     * @param id
     */
    @Delete("delete from t_user_role where user_id = #{user_id}")
    void deleteUserByRole(Integer id);


    /**
     * 删除id值得用户
     * @param id
     */
    @Delete("delete from t_user where id = #{id}")
    void deleteUser(Integer id);

}
